﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using aier.xrm.wcf;
using log4net;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class ScreenCustomerBll
    {
        public ILog log { get; set; }
        public ResResult resResult { get; set; }
        public CommonWcf commonWcf { get; set; }
        public ScreenCustomerDal screencustomerDal { get; set; }

        #region 查询 
        /// <summary>
        /// 根据筛查ID,客户ID查询筛查客户对像视图
        /// 原程序 ScreeningServiceImpl-> selectScCustomer(String scid, String customerid)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerId">潜客ID</param>
        /// <returns></returns>
        public ScreenCustomerView GetScreenCustomerByScreenIdAndCustomerId(Guid scid, Guid customerId)
        {
            var screenCustomerView = screencustomerDal.GetScreenCustomerByScreenIdAndCustomerId(scid, customerId);
            screenCustomerView.birthdate = screenCustomerView.birthdate.HasValue ? screenCustomerView.birthdate.Value.AddHours(8) : screenCustomerView.birthdate;
            return screenCustomerView;
        }

        /// <summary>
        /// 根据筛查ID查询进行中的筛查客户对像视图
        /// 原程序 ScreeningServiceImpl-> selectScCustomerNotCompleteByScid(String scid)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <returns></returns>
        public IEnumerable<ScreenCustomerView> GetScreenCustomerNotCompleteByScreenId(Guid scid)
        {
            return screencustomerDal.GetScreenCustomerNotCompleteByScreenId( scid);
        }

        /// <summary>
        /// 根据筛查ID查询所有筛查客户对像视图
        /// 原程序 ScreeningServiceImpl-> selectScCustomerAllState(String scid)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <returns></returns>
        public IEnumerable<ScreenCustomerView> GetAllScreenCustomerByScreenId(Guid scid)
        {
            return screencustomerDal.GetAllScreenCustomerByScreenId( scid);
        }

        /// <summary>
        /// 根据筛查ID和状态查询筛查客户列表
        /// 原程序 ScreeningServiceImpl-> selectScCustomerByScidAndState(String scid, String scstate)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="scstate">筛查ID</param>
        /// <returns></returns>
        public IEnumerable<ScreenCustomerView> GetScreenCustomerByScreenIdAndState(Guid scid, int scstate)
        {
            return screencustomerDal.GetScreenCustomerByScreenIdAndState(scid,scstate);
        }

        /// <summary>
        /// 根据筛查ID和客户筛查状态为进行中,(姓名,电话,身份证,学号模糊查询)查询筛查客户列表
        /// 原程序 ScreeningServiceImpl-> selectScCustomerByScidAndKey(String scid, String key) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="key">筛查ID</param>
        /// <returns></returns>
        public IEnumerable<ScreenCustomerView> GetScreenCustomerByScreenIdAndKey(Guid scid, string key)
        {
            if (CommonUtil.IsIDCard(key))
            {
                return screencustomerDal.GetScreenCustomerByScreenIdAndCitizenId(scid, key);
            }
            else if (CommonUtil.IsMobile(key))
            {
                return screencustomerDal.GetScreenCustomerByScreenIdAndPhone(scid, key);
            }
            else
            {
                return screencustomerDal.GetScreenCustomerByScreenIdAndFullNameClassNumber(scid, key);
            }
        }

        /// <summary>
        /// 根据筛查ID和客户筛查状态,(姓名,电话,身份证,学号模糊查询)查询筛查客户列表
        /// 原程序 ScreeningServiceImpl-> selectByScidStateAndKey(String scid, Integer scstate, String key, int fromIndex, int toIndex) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="scstate">筛查客户状态</param>
        /// <param name="key">筛查ID</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <returns></returns>
        public PageResult<ScreenCustomerView> GetScreenCustomerByScreenIdAndSCStaceAndKeyPaging(Guid scid, int scstate, string key, int pageIndex, int pageSize)
        {
            int tmpPageSize = pageSize + 1;
            IEnumerable<ScreenCustomerView> views = null;
            PageResult<ScreenCustomerView> pageResult = new PageResult<ScreenCustomerView>();

            if(string.IsNullOrEmpty(key))
            {
                views = screencustomerDal.GetScreenCustomerByScreenIdAndSCStatePaging(scid, scstate, pageIndex, tmpPageSize);
                pageResult.datas = views.Skip(0).Take(pageSize);
                pageResult.existsnextdata = views.Count() > pageSize;
                pageResult.total = screencustomerDal.GetCountScreenCustomerByScreenIdAndSCStatePaging(scid, scstate);
            }
            else
            {
                if (CommonUtil.IsIDCard(key))
                {
                    views = screencustomerDal.GetScreenCustomerByScreenIdAndSCStaceAndCitizenidPaging(scid, scstate, key, pageIndex, tmpPageSize);
                    pageResult.datas = views.Skip(0).Take(pageSize);
                    pageResult.existsnextdata = views.Count() > pageSize;
                    pageResult.total = screencustomerDal.GetCountScreenCustomerByScreenIdAndSCStaceAndCitizenidPaging(scid, scstate, key);
                }
                else if (CommonUtil.IsMobile(key))
                {
                    views = screencustomerDal.GetScreenCustomerByScreenIdAndSCStaceAndPhonePaging(scid, scstate, key, pageIndex, tmpPageSize);
                    pageResult.datas = views.Skip(0).Take(pageSize);
                    pageResult.existsnextdata = views.Count() > pageSize;
                    pageResult.total = screencustomerDal.GetCountScreenCustomerByScreenIdAndSCStaceAndPhonePaging(scid, scstate, key);
                }
                else
                {
                    views = screencustomerDal.GetScreenCustomerByScreenIdAndSCStaceAndFullNameClassNumberPaging(scid, scstate, key, pageIndex, tmpPageSize);
                    pageResult.datas = views.Skip(0).Take(pageSize);
                    pageResult.existsnextdata = views.Count() > pageSize;
                    pageResult.total = screencustomerDal.GetCountScreenCustomerByScreenIdAndSCStaceAndFullNameClassNumberPaging(scid, scstate, key);
                }
            }

            return pageResult;
        }

        /// <summary>
        /// 根据筛查客户ID查询筛查客户
        /// 原程序 ScreeningServiceImpl-> selectScCustomer(String customerId)
        /// </summary>
        /// <param name="customerid">筛查客户ID</param>
        /// <returns></returns>
        public IEnumerable<ScreenCustomerView> GetScreenCustomerByCustomerId(Guid customerid)
        {

            return screencustomerDal.GetScreenCustomerByCustomerId( customerid);
        }

        /// <summary>
        /// 根据筛查编号,客户名称查询筛查报告
        /// 原程序 ScreeningServiceImpl-> selectScCustomerByscNum(String scNum, String scName)
        /// </summary>
        /// <param name="scnum">筛查编号</param>
        /// <param name="fullname">客户名称</param>
        /// <returns></returns>
        public IEnumerable<ScreenCustomerView> GetScreenCustomerBySCNum(string scnum, string fullname)
        {
            return screencustomerDal.GetScreenCustomerBySCNum( scnum,  fullname);
        }

        /// <summary>
        /// 根据筛查编号,客户名称查询重名客户数量
        /// 原程序 ScreeningServiceImpl-> countCustomernameByScid(String scid, String customername)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="fullname">客户名称</param>
        /// <returns></returns>
        public int GetCountScreenCustomerByScidAndFullName(string scid, string fullname)
        {
            return screencustomerDal.GetCountScreenCustomerByScidAndFullName(scid,  fullname);
        }

        /// <summary>
        /// 查询筛查完成的客户id
        /// 原程序 ScreeningServiceImpl-> selectFinshedCustomerId(String school, String classnum) 
        /// </summary>
        /// <param name="school">学校ID</param>
        /// <param name="classnum">班级号</param>
        /// <returns></returns>
        public IEnumerable<dynamic> GetScreenCustomerBySchoolAndClassNum(Guid school, string classnum)
        {
            return screencustomerDal.GetScreenCustomerBySchoolAndClassNum( school,  classnum);
        }

        /// <summary>
        /// 根据潜客ID与筛查ID查询筛查客户关系是否存在
        /// </summary>
        /// <param name="customerId">潜客ID</param>
        /// <param name="screenId">筛查ID</param>
        /// <returns></returns>
        public bool ExistsScreenCustomer(Guid customerId, Guid screenId)
        {
            return screencustomerDal.ExistsScreenCustomer(customerId, screenId);
        }

        #endregion

        #region 更新

        /// <summary>
        /// 创建筛查客户
        /// 原程序 ：ScreeningServiceImpl-> saveScCustomer4School(DoctorUser user, String scid, String teamid, String customerid, String ownerid) 
        /// </summary>
        /// <param name="customer">筛查客户对像</param>
        /// <returns></returns>
        public ResResult CreateCustomer(ScreenCustomer customer)
        {
            Guid entityId = Guid.Empty;
            Entity entity = customer.AsCrmEntity();
            //entity.Id = customer.new_sc_customerid;
            try
            {
                if (Guid.Empty.Equals(customer.userid))
                {
                    entityId = commonWcf.CreateEntity(entity);
                }
                else
                {
                    entityId = commonWcf.CreateEntityProxy(customer.userid, entity);
                }
                return resResult.SuccessResult(entityId);
            }
            catch (Exception ex)
            {
                //log.Error("ScreenCustomerBll-> CreateCustomer: " + ex.Message);
                return resResult.FailResult("ScreenCustomerBll-> CreateCustomer: " + ex.Message);
            }
        }


        /// <summary>
        /// 更新筛查客户
        /// </summary>
        /// <param name="customer">筛查客户对像</param>
        /// <returns></returns>
        public ResResult UpdateScreenCustomer(ScreenCustomer customer)
        {
            try
            {
                Entity entity = customer.AsCrmEntity();
                if (Guid.Empty.Equals(customer.userid))
                {
                    commonWcf.UpdateEntityById(entity);
                }
                else
                {
                    commonWcf.UpdateEntityProxyById(customer.userid, entity);
                }

                return resResult.SuccessResult();
            }
            catch (Exception ex)
            {
                log.Error("ScreenCustomerBll-> UpdateScreenCustomer: " + ex.Message);
                return resResult.FailResult("ScreenCustomerBll-> UpdateScreenCustomer: " + ex.Message);
            }
        }
        public ResResult UpdateScreenCustomerByAdmin(ScreenCustomer customer)
        {
            try
            {
                Entity entity = customer.AsCrmEntity();
                
                    commonWcf.UpdateEntityById(entity);
                                return resResult.SuccessResult();
            }
            catch (Exception ex)
            {
                log.Error("ScreenCustomerBll-> UpdateScreenCustomer: " + ex.Message);
                return resResult.FailResult("ScreenCustomerBll-> UpdateScreenCustomer: " + ex.Message);
            }
        }

        #endregion

        #region 根据潜客Id获取信息
        public IEnumerable<dynamic> GetScreenCustomerByLeadId(Guid leadId)
        {

            var data = screencustomerDal.GetScreenCustomerByLeadId(leadId);
            return data;
        }
        #endregion
    }
}
